package com.example.android.helloactivity;
import android.util.Log;


public class WorkoutThread{ 

	ChessView myDraw;
	int quit;
	int aiLevel;
	
	public class ValueOfStep {
		int px;
		int py;
		int selfCore;
		int rivalCore;
		int whowin;
		int deepwin;
		
		ValueOfStep(){
			px = 0;
			py = 0;
			selfCore = 0;
			rivalCore = 0;
		}
	}
	ValueOfStep steps[];
	ValueOfStep step_simple;
	WorkoutThread(ChessView obj,int aiLevel){
		
		myDraw = obj;
		quit = 0;
		this.aiLevel=aiLevel;
		steps = new ValueOfStep[myDraw.numPoint];
		
		for(int i=0;i<myDraw.numPoint;i++){
			
			steps[i]=new ValueOfStep();
		}
		
		step_simple=new ValueOfStep();
	}
	
	public int value(int px, int py){
		int ret = 0;
		int num = 0;
		
		num = myDraw.first.numOfQizi(px, py, 0, -1,myDraw.second);
		ret+=myDraw.first.calcValue(num, 0);
		num = myDraw.first.numOfQizi(px, py, -1, 0,myDraw.second);
		ret+=myDraw.first.calcValue(num, 0);
		num = myDraw.first.numOfQizi(px, py, -1, -1,myDraw.second);
		ret+=myDraw.first.calcValue(num, 0);
		num = myDraw.first.numOfQizi(px, py, +1, -1,myDraw.second);
		ret+=myDraw.first.calcValue(num, 0);
		
		num = myDraw.second.numOfQizi(px, py, 0, -1,myDraw.first);
		ret+=myDraw.second.calcValue(num, 1);
		num = myDraw.second.numOfQizi(px, py, -1, 0,myDraw.first);
		ret+=myDraw.second.calcValue(num, 1);
		num = myDraw.second.numOfQizi(px, py, -1, -1,myDraw.first);
		ret+=myDraw.second.calcValue(num, 1);
		num = myDraw.second.numOfQizi(px, py, +1, -1,myDraw.first);
		ret+=myDraw.second.calcValue(num, 1);
		
		return ret;
	}
	
	//depends on the role to workout the value of the point
	//flag: 0--person  1--computer
	public int valueOfRole(Situation self,Situation rival,int px, int py,int flag,int debug){
		int ret = 0;
		int num = 0;
		//flag=1:self attach get cores,flag=0: attached by rival get cores
		/*num = self.numOfQizi(px, py, 0, -1,rival);
		ret += self.calcValue(num, flag);
		num = self.numOfQizi(px, py, -1, 0,rival);
		ret += self.calcValue(num, flag);
		num = self.numOfQizi(px, py, -1, -1,rival);
		ret += self.calcValue(num, flag);
		num = self.numOfQizi(px, py, +1, -1,rival);
		ret += self.calcValue(num, flag);*/
		
		int tempValue=0;
		int shuangSha=0;
		int hasFour=0;
		for(int i=-1;i<=2;i++){
			
			int a=i;
			int b=1;
			if(2==i){
				a=1;
				b=0;
			}
			num = self.numOfQizi(px, py, a, b,rival);
			tempValue = self.calcValue(num, flag);
			ret+=tempValue;
			
			if(4==num && self.emptyNum>=1){

				hasFour++;
				if(1==self.middleEmpty){
					shuangSha+=1;
				}else{
					shuangSha+=self.emptyNum;
				}
				//Log.d("Debug","flag:"+flag+"  ("+px+","+py+")"+"  44444num="+num+"    emptyNum="+self.emptyNum+"    shuangSha:"+shuangSha);
			}
			
			if(3==num && self.emptyNum==2){
				shuangSha+=1;
				//Log.d("Debug","flag:"+flag+"  ("+px+","+py+")"+"  33333num="+num+"    emptyNum="+self.emptyNum+"    shuangSha:"+shuangSha);
			}
			
			if(1==debug){
				//Log.d("Debug","("+px+","+py+")"+"zi:"+num+" kong:"+self.emptyNum+"value:"+tempValue+" total:"+ret);
			}
		}
		
		if(shuangSha >= 2 && ret < 50000000){
			
			ret=(hasFour*6000000+30000)*8/(flag==1?8:10)+ret;
			//Log.d("Debug","("+px+","+py+")"+"Attention please!shuangSha modify!"+"total:"+ret);
		}
		
		return ret;
	}
	

	public void WhereToSet_Simple(int role){
		
        int maxValue = -1;
        int attackCore = 0;
        int defenceCore = 0;

    		
        step_simple.px = -1;
        step_simple.py = -1;
        step_simple.selfCore = 0;
        step_simple.rivalCore = 0;
        step_simple.whowin = 0;
        step_simple.deepwin = 1000;
        
        //
        //calc the most best step,find the three best step
        //
        
        for(int i=0;i<ChessView.QIPANSIZE;i++){
        	
	        for(int j=0;j<ChessView.QIPANSIZE;j++){
	
	        	if(1 == myDraw.first.situation[i][j] || 1 == myDraw.second.situation[i][j]){
	        		continue;
	        	}
	        	//2:computer,1:person
	        	if(2==role){
	        	
	        		attackCore = valueOfRole(myDraw.second,myDraw.first,i,j,1,0);
	        		defenceCore = valueOfRole(myDraw.first,myDraw.second,i,j,0,0);
	        	}else{
	        		attackCore = valueOfRole(myDraw.second,myDraw.first,i,j,0,0);
	        		defenceCore = valueOfRole(myDraw.first,myDraw.second,i,j,1,0);
	        	}
	        		
	        	if(attackCore+defenceCore>maxValue){
	        			
	        		maxValue=attackCore+defenceCore;
	        		step_simple.px=i;
	        		step_simple.py=j;
	        		step_simple.selfCore=attackCore+defenceCore;
	        	}
	        }
        }
	}
	
	public void WhereToSet(){
		
        int maxValue = -1;
        int attackCore = 0;
        int defenceCore = 0;
        //zero all the points to empty
    	for(int k=0;k<myDraw.numPoint;k++){
    		
			steps[k].px = -1;
			steps[k].py = -1;
			steps[k].selfCore = 0;
			steps[k].rivalCore = 0;
			steps[k].whowin = 0;
			steps[k].deepwin = 1000;
    	}
        
        //
        //calc the most best step,find the three best step
        //
        
        for(int i=0;i<ChessView.QIPANSIZE;i++){
        	
	        for(int j=0;j<ChessView.QIPANSIZE;j++){
	
	        	if(1 == myDraw.first.situation[i][j] || 1 == myDraw.second.situation[i][j]){
	        		continue;
	        	}
	        	
	        	attackCore = valueOfRole(myDraw.second,myDraw.first,i,j,1,0);
	        	defenceCore = valueOfRole(myDraw.first,myDraw.second,i,j,0,0);
	        	//find the three most biggest value,exchange the smallest one,to do
	        	int smallest = 0;
	        	for(int k=0;k<myDraw.numPoint&&k+1<myDraw.numPoint;k++){
	        		
	        		if(steps[smallest].selfCore+steps[smallest].rivalCore>steps[k+1].selfCore+steps[k+1].rivalCore){
	        			
	        			smallest = k+1;
	        		}
	        	}
	        	if(attackCore+defenceCore>steps[smallest].selfCore+steps[smallest].rivalCore){
	        		
        			steps[smallest].px = i;
        			steps[smallest].py = j;
        			steps[smallest].selfCore = attackCore+defenceCore;
	        	}
	        }
        }
        
        int x = steps[0].px;
        int y = steps[0].py;
        //record who win
        int deepWin=1000000;

        for(int k=0;k<myDraw.numPoint;k++){
        	
        	int temp=steps[k].selfCore+steps[k].rivalCore;
        	Log.d("Debug","k="+k+",px="+steps[k].px+",py="+steps[k].py+",Core="+temp+"\n\n\n");
        	
        	//begin forecase the future
    		step_simple.px=steps[k].px;
    		step_simple.py=steps[k].py;
        	int i=0;
        	int number=0;
	        //Log.d("Debug","11=================second index="+myDraw.second.index+"===============\n\n\n\n");
	        //Log.d("Debug","11=================first index="+myDraw.first.index+"===============\n\n\n\n");
        	for(i=0;i<aiLevel;i++){
        		
    			myDraw.second.history[myDraw.second.index].px = step_simple.px;
    			myDraw.second.history[myDraw.second.index].py = step_simple.py;
    			myDraw.second.history[myDraw.second.index].invalid = 2;//this value should not be 1,temply for 2,or it is paint error
    			myDraw.second.situation[myDraw.second.history[myDraw.second.index].px][myDraw.second.history[myDraw.second.index].py]=1;
    			myDraw.second.index+=1;
    			number++;
    			Log.d("Debug","PCPCPC========x="+step_simple.px+" y="+step_simple.py);
    			if(myDraw.second.judgeWin(myDraw.second.history[myDraw.second.index-1].px,myDraw.second.history[myDraw.second.index-1].py)){
					
    				//record it first
    				if(deepWin>i*2){
					
    					deepWin=i*2;
    					x=steps[k].px;
    					y=steps[k].py;
    					Log.d("Debug","********computer win========deepWin_computer="+deepWin+"  x="+x+"  y="+y+"\n");
    				}
					break;
				}
    			
    			//todo,person's turn
    			WhereToSet_Simple(1);
    			myDraw.first.history[myDraw.first.index].px = step_simple.px;
    			myDraw.first.history[myDraw.first.index].py = step_simple.py;
    			myDraw.first.history[myDraw.first.index].invalid = 2;
    			myDraw.first.situation[myDraw.first.history[myDraw.first.index].px][myDraw.first.history[myDraw.first.index].py]=1;
    			myDraw.first.index+=1;
    			number++;
    			Log.d("Debug","RENREN========x="+step_simple.px+" y="+step_simple.py);
    			if(myDraw.first.judgeWin(myDraw.first.history[myDraw.first.index-1].px,myDraw.first.history[myDraw.first.index-1].py)){
    				//record it first
    				
					if(deepWin>i*2+1){
						//person will win,so i can't put it here,zero it's score
						steps[k].rivalCore=0;
						steps[k].selfCore=0;
						Log.d("Debug","********person win====remove it====deepWin_person="+deepWin+"  x="+x+"  y="+y+"\n");
					}
					break;
    			}
    			//computer's turn
    			WhereToSet_Simple(2);
        	}
        	
        	//after the for,rollback
	        //Log.d("Debug","22=================second index="+myDraw.second.index+"===============\n\n\n\n");
	        //Log.d("Debug","22=================first index="+myDraw.first.index+"===============\n\n\n\n");
        	for(int q=0;q<number;q++){
        		
        		myDraw.second.index-=1;
    			myDraw.second.situation[myDraw.second.history[myDraw.second.index].px][myDraw.second.history[myDraw.second.index].py]=0;
    			myDraw.second.history[myDraw.second.index].px = 0;
    			myDraw.second.history[myDraw.second.index].py = 0;
    			myDraw.second.history[myDraw.second.index].invalid = 0;	    				        		
        		q++;
        		if(q>=number){
        			
        			break;
        		}
        		
        		myDraw.first.index-=1;
    			myDraw.first.situation[myDraw.first.history[myDraw.first.index].px][myDraw.first.history[myDraw.first.index].py]=0;
    			myDraw.first.history[myDraw.first.index].px = 0;
    			myDraw.first.history[myDraw.first.index].py = 0;
    			myDraw.first.history[myDraw.first.index].invalid = 0;
        	}
        	
	        //Log.d("Debug","33=================second index="+myDraw.second.index+"===============\n\n\n\n");
	        //Log.d("Debug","33=================first index="+myDraw.first.index+"===============\n\n\n\n");
        }
        
        maxValue = -1000000000;
		if(deepWin==1000000){
			
			//find the best of the three solution,find the best point,x and y
	        for(int k=0;k<myDraw.numPoint;k++){
	        	
	        	//find the best of the three solution,find the best point,x and y
	        	if(maxValue<steps[k].selfCore+steps[k].rivalCore){
	        		
	        		maxValue=steps[k].selfCore+steps[k].rivalCore;
	        		x=steps[k].px;
	        		y=steps[k].py;
	        	}
	        }
		}else{
			
			Log.d("Debug","x="+x+" y="+y+"aaaaaaaaaaaaaaaaaaaaaaaaaa   deepWin="+deepWin+"    aaaaaaaaaaaaa\n\n");
		}
        Log.d("Debug","*****************index="+myDraw.second.index+" using*** "+"x="+x+"  y="+y+"\n\n\n\n");	
		myDraw.second.history[myDraw.second.index].px = x;
		myDraw.second.history[myDraw.second.index].py = y;
		myDraw.second.history[myDraw.second.index].invalid = 1;
		myDraw.second.situation[myDraw.second.history[myDraw.second.index].px][myDraw.second.history[myDraw.second.index].py]=1;
		myDraw.second.index += 1;
	}
} //end of workoutthread